package com.wolfking.converter.transform.base;


import com.google.common.collect.Sets;
import com.wolfking.converter.transform.FieldTransform;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

public class CharFieldTransform implements FieldTransform<Character> {

    private final Set<Class<?>> classes = Sets.newHashSet(Character.class, char.class);

    @Override
    public boolean canTransform(Class<?> fieldType) {
        return classes.contains(fieldType);
    }

    @Override
    public Character transform(Object value) {
        for (Class<?> clazz : classes) {
            if (clazz.isAssignableFrom(value.getClass())) {
                return (char) value;
            }
        }
        if (StringUtils.isNotBlank(value.toString())) {
            return value.toString().trim().charAt(0);
        } else {
            throw new IllegalStateException("char 转换异常");
        }
    }
}
